package com.itluma.myshopping.dao.impl;

import com.itluma.myshopping.dao.CartDao;
import com.itluma.myshopping.entity.CartPro;
import com.itluma.myshopping.entity.Product;
import com.itluma.myshopping.utils.JdbcUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MS
 * @date 2021/3/15 19:25
 **/

public class CartDaoImpl implements CartDao{
    private static QueryRunner qr = new QueryRunner (JdbcUtils.getDataSource ());
/*    private static Map<String, String> map = new HashMap<> ();

    static{
        map.put ("c_id", "cid");
        map.put ("u_id", "uid");
        map.put ("p_id", "pid");
        map.put ("c_count", "ccount");
        map.put ("c_num", "cnum");
    }*/

    /**
     * 根据 用户id 查找所有购物车记录
     * @param uid
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<CartPro> findByUid(String uid) throws SQLException, InvocationTargetException, IllegalAccessException{
        String sql = "SELECT c_id cid,u_id uid,c.p_id pid,c_count ccount,c_num cnum,p.p_name pname ,p.p_price pprice FROM cart c LEFT JOIN product p ON c.p_id = p.p_id  WHERE c.u_id = ?";


        List<Map<String, Object>> query = qr.query (sql, new MapListHandler (), uid);

        List<CartPro> list = new ArrayList<> ();

        for (Map<String, Object> objectMap : query) {

            CartPro cartPro = new CartPro ();
            Product product = new Product ();

            BeanUtils.populate (cartPro, objectMap);
            BeanUtils.populate (product, objectMap);
            cartPro.setProduct (product);
            list.add (cartPro);
        }



        return null;

    }

    @Override
    public Product find(String uid){
        return null;
    }


}